﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Job;
using CE.HR;

public partial class HR_job_add_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.HRId] == null)
        {
            Response.Redirect("login.aspx");
        }
        if (!IsPostBack)
        {
            txtEndDate_CalendarExtender.StartDate = DateTime.Now;

            if (Request.Params["jid"] != null)
            {
                int _jobid = int.Parse(Request.Params["jid"].ToString());
                Job _job = new Job();
                JobDB _jobdb = new JobDB();
                _job = _jobdb.GetJobByID(_jobid);
                if (_job != null)
                {
                    txtJobTitle.Text = _job.JobTitle;
                    txtJobDescription.Text = _job.JobDescription;
                    txtResponsibilities.Text = _job.Responsibilities;
                    txtRequirements.Text = _job.Requirements;
                    txtEndDate.Text = _job.EndDate.ToString("dd/MM/yyyy");
                    txtStartDate.Text = _job.StartDate.ToString("dd/MM/yyyy");
                    ddlStatus.SelectedValue = _job.Status.ToString().ToLower();

                    btnSubmit.Text = "Update";
                }

                lblTitle.Text = "Edit Job Post";
            }
            else
            {
                lblTitle.Text = "Add Job Post";
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Job _job = new Job();
            JobDB _jobdb = new JobDB();

            if (Request.Params["jid"] != null)
            {
                int _jobid = int.Parse(Request.Params["jid"].ToString());
                _job = _jobdb.GetJobByID(_jobid);
            }
            else
            {
                HRAccountDB _accountdb = new HRAccountDB();
                HRAccount _account = _accountdb.GetAccount(Int64.Parse(Session[CE.Global.General.HRId].ToString()));

                _job.Country = _account.Country;
                _job.CreatedBy = _account.HRID;
            }

            _job.JobTitle = txtJobTitle.Text;
            _job.JobDescription = txtJobDescription.Text;
            _job.Responsibilities = txtResponsibilities.Text;
            _job.Requirements = txtRequirements.Text;
            _job.Status = bool.Parse(ddlStatus.SelectedValue);
            _job.EndDate = DateTime.ParseExact(txtEndDate.Text, "dd/MM/yyyy", null);
            _job.StartDate = DateTime.ParseExact(txtStartDate.Text, "dd/MM/yyyy", null);

            Int64 result = _jobdb.InsertUpdate(_job);
            if (result > 0)
            {
                Response.Redirect("job-post.aspx");
            }
        }
    }
    protected void btnPreview_Click(object sender, EventArgs e)
    {
        Session["JobTitle"] = txtJobTitle.Text;
        Session["JobDescription"] = txtJobDescription.Text;
        Session["Responsibilities"] = txtResponsibilities.Text;
        Session["Requirements"] = txtRequirements.Text;
        Session["Status"] = bool.Parse(ddlStatus.SelectedValue);
        Session["EndDate"] = DateTime.ParseExact(txtEndDate.Text, "dd/MM/yyyy", null);
        Session["StartDate"] = DateTime.ParseExact(txtStartDate.Text, "dd/MM/yyyy", null);

        Response.Redirect("job-preview.aspx");
    }
}
